<!DOCTYPE html>
<title>Credential Manager: get() with custom origins.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>

// For tests that require custom-set origins.

const VALID_ORIGIN_RPID_PAIRS = [
    { 'origin': 'https://google.test:8443',
      'rpId': 'google.test' },
    {'origin': 'https://subdomain.example.test:8443',
      'rpId': 'example.test' },
    {'origin': 'https://subdomain.example.test:8443',
      'rpId': 'subdomain.example.test' },
    {'origin': 'https://localhost:8443',
      'rpId': 'localhost' },
];

for (let test of VALID_ORIGIN_RPID_PAIRS) {
  promise_test(t => {
    let eventWatcher = new EventWatcher(t, window, "message");

    var w = window.open(test.origin
        + "/credentialmanager/resources/publickey-get-helper.html?rpId="
        + test.rpId);
    return eventWatcher.wait_for("message").then(message => {
        assert_equals(message.data, "SUCCESS");
    });
  }, "navigator.credentials.get({publicKey}) in '" +  test.origin
      + "' with valid |rpId| '" + test.rpId + "' should succeed.");
}

const INVALID_ORIGIN_RPID_PAIRS = [
    { 'origin': 'https://google.test:8443',
      'rpId': 'localhost' },
    { 'origin': 'https://google.test:8443',
      'rpId': 'foo.google.test' },
    { 'origin': 'https://google.test:8443',
      'rpId': null },
    { 'origin': 'https://google.test:8443',
      'rpId': String(0) },
    { 'origin': 'https://google.test:8443',
      'rpId': 'test' },
    { 'origin': 'https://google.test:8443',
      'rpId': '' },
];

for (let test of INVALID_ORIGIN_RPID_PAIRS) {
  promise_test(t => {
    let eventWatcher = new EventWatcher(t, window, "message");

    var w = window.open(test.origin
        + "/credentialmanager/resources/publickey-get-helper.html?rpId="
        + test.rpId);

    return eventWatcher.wait_for("message").then(message => {
        assert_equals(message.data, "SecurityError");
    });
  }, "navigator.credentials.get({publicKey}) in '" +  test.origin
      + "' with invalid |rpId| '" + test.rpId + "' should fail.");
}

</script>
